package com.media.themovieworld.screen.menu;

import android.app.ProgressDialog;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.Response;
import com.media.themovieworld.R;
import com.media.themovieworld.common.fragment.MovieWorldScreen;
import com.media.themovieworld.util.AppSettingPrefference;
import com.media.themovieworld.util.FacebookUtils;
import com.media.themovieworld.util.FacebookUtils.FacebookListener;
import com.media.themovieworld.util.dialog.MovieDialogAbout;

public class LeftMenuScreen extends MovieWorldScreen implements OnClickListener{

	private LinearLayout btnSetting;
	private LinearLayout btnHelp;
	private LinearLayout btnInfo;
	private Button btnLogin;
	
	private LinearLayout layoutLogin;
	private LinearLayout layoutUserInfo;
	private ImageView imgAvatar;
	private TextView tUserName;
	private Button btnLogout;
	
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.tmw__sliding_menu_main, container,false);
		btnSetting  = (LinearLayout) v.findViewById(R.id.tmw__sliding_menu_group_config_layout_setting);
		btnHelp		= (LinearLayout) v.findViewById(R.id.tmw__sliding_menu_group_config_layout_help);
		btnInfo		= (LinearLayout) v.findViewById(R.id.tmw__sliding_menu_group_config_layout_app_info);
		btnLogin	= (Button) v.findViewById(R.id.tmw__sliding_menu_group_user_info_button_login);
		layoutLogin = (LinearLayout) v.findViewById(R.id.tmw__sliding_menu_group_user_info_layout_login);
		layoutUserInfo = (LinearLayout) v.findViewById(R.id.tmw__sliding_menu_group_user_info_layout_user_info);
		imgAvatar	= (ImageView) v.findViewById(R.id.tmw__sliding_menu_group_user_info_image_view_avatar);
		tUserName	= (TextView) v.findViewById(R.id.tmw__sliding_menu_group_user_info_textview_username);
		btnLogout	= (Button) v.findViewById(R.id.tmw__sliding_menu_group_user_info_button_logout);
		
		btnSetting.setOnClickListener(this);
		btnHelp.setOnClickListener(this);
		btnInfo.setOnClickListener(this);
		btnLogin.setOnClickListener(this);
		btnLogout.setOnClickListener(this);
		return v;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tmw__sliding_menu_group_config_layout_setting:
			removeMenuIfExist();
			getMainActivity().switchContent(new SettingScreen(), true);
			getMainActivity().toggleMenu();
			break;
		case R.id.tmw__sliding_menu_group_config_layout_help:
			removeMenuIfExist();
			getMainActivity().switchContent(new HelpScreen(), true);
			getMainActivity().toggleMenu();
			break;
		case R.id.tmw__sliding_menu_group_config_layout_app_info:
//			getMainActivity().switchContent(new InfoScreen(), true);
//			getMainActivity().toggleMenu();
			new MovieDialogAbout(getActivity()).show();
			break;
		case R.id.tmw__sliding_menu_group_user_info_button_login:
			loginFacebook();
			break;
		case R.id.tmw__sliding_menu_group_user_info_button_logout:
			logoutFacebook();
			break;
		default:
			break;
		}
	}
	
	public void removeMenuIfExist(){
		Fragment frag = getMainActivity().getTopStack();
		if(frag == null) return;
		if (frag instanceof SettingScreen
				|| frag instanceof HelpScreen) {
			getMainActivity().getSupportFragmentManager().popBackStack();
		}
	}
	
	private void logoutFacebook(){
		FacebookUtils.logoutFaceBook(getActivity());
		layoutLogin.setVisibility(View.VISIBLE);
		layoutUserInfo.setVisibility(View.GONE);
	}
	
	private void loginFacebook(){
		FacebookUtils.loginToFacebook(getActivity(), new ProgressDialog(getActivity()), new FacebookListener() {
			
			@Override
			public void onSuccess(Response pResponse) {
				if(pResponse!=null){
					String fbId = (String) pResponse.getGraphObject().getProperty("id");
		            String fbName = (String) pResponse.getGraphObject().getProperty("name");
		            AppSettingPrefference.getInstance().setFacebookId(fbId);
		            
		            if (fbName!=null) {
						layoutLogin.setVisibility(View.GONE);
						layoutUserInfo.setVisibility(View.VISIBLE);
						tUserName.setText(fbName);
					}
				}
			}
			
			@Override
			public void onFail() {
				layoutLogin.setVisibility(View.VISIBLE);
				layoutUserInfo.setVisibility(View.GONE);
			}
		});
	}

}
